class SearchersController < ApplicationController

  # 只允许登录用户搜索
  before_filter :login_required

  # 常规sql查询来搜索
  def search
    query = params[:keywords]
    @keywords = query
    unless query.blank?
      if !query.to_s.strip.empty?
        tokens = query.split.collect {|c| "%#{c.downcase}%"}
        @schools = School.paginate(:per_page => 20,:page => params[:page],
                    :conditions => [(["(LOWER(address) LIKE ? OR LOWER(traffic_description) LIKE ? 
                    OR LOWER(title) LIKE ?)"] * tokens.size).join(" AND ") + 
                    "AND is_hidden = '0'",
                    * tokens.collect { |token| [token] * 3 }.flatten],
                                      :order => "created_at DESC")
      else
        @schools = []
      end
    end
  end

  def find_local
    query = params[:keywords]
    @keywords = query
    unless query.blank?
      if !query.to_s.strip.empty?
        tokens = query.split.collect {|c| "%#{c.downcase}%"}
        @events = Event.paginate(:per_page => 20,:page => params[:page],
                    :conditions => [(["(LOWER(location) LIKE ? OR LOWER(content) LIKE ? 
                    OR LOWER(title) LIKE ?)"] * tokens.size).join(" AND ") + 
                    "AND is_hidden = '0'",
                    * tokens.collect { |token| [token] * 3 }.flatten],
                                      :order => "created_at DESC")
      else
        @schools = []
      end
    end
  end

end